﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Omu.AwesomeMvc;
using TCM;
using TCM.Models;

namespace TCM.Controllers{
    public class StudentLookupController : Controller
    {
        private TCMContext Db = new TCMContext();
        public ActionResult Search(string search, int page)
        {
          const int PageSize = 10;
            //selected = selected ?? new Guid[] { };
            search = (search ?? "").ToLower().Trim();

            var items = Db.Students.ToList().Skip((page - 1) * PageSize).Take(PageSize);//.Where(o => o.Name.ToLower().Contains(search) && (!selected.Contains(o.StudentID)));

            return Json(new AjaxListResult
            {
                Items = items.Select(o => new KeyContent(o.StudentID, o.Name.ToString())),
                More = items.Count() > page * PageSize
            });
        }

        public ActionResult GetItem(Guid? v)
        {
            var o = v == null || v == Guid.Empty ? new Student() : Db.Students.Find(v);

            return Json(new KeyContent(o.StudentID, o.Name));
        }
    }

    public class StudentsAjaxListController : Controller
    {
        private TCMContext Db = new TCMContext();
        public ActionResult Search(string search, int page, bool isTheadEmpty, int? pageSize)
        {
            pageSize = pageSize ?? 10;
            search = (search ?? "").ToLower().Trim();
            var list = Db.Students.Where(o => o.Name.ToLower().Contains(search))
                .OrderByDescending(o => o.StudentID);

            var r = new AjaxListResult
                        {
                            Content = this.RenderView("ListItems/_Student", list.Skip((page - 1) * pageSize.Value).Take(pageSize.Value)),
                            More = list.Count() > page * pageSize
                        };
            if (isTheadEmpty) r.Thead = this.RenderView("ListItems/_StudentThead");
            return Json(r);
        }
    }

}